﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    编辑条目
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
   $(document).ready(function () {
      $("#Commit").click(function () {
         if ($("#Title").attr("value") == "") {
            $("#TitleValidator").attr("innerHTML", "提交的条目中至少标题不能为空！");
            return false;
         }
         $("#TitleValidator").attr("innerHTML", "");
         return true;
      });
      $("#categories").find("div").click(function () {
         var text = $(this).find("a").attr("innerHTML");
         var id = $(this).find("input").attr("value");
         $("#CategoryName").attr("value", text);
         $("#CategoryID").attr("value", id);
         return false;
      });
   });
</script>
<% Action<KnowledgeEntity.Category> renderCategories = null; %>
<% renderCategories = (category) =>
   { 
      if (category == null) return; 
      if (category.ChildrenCount > 0)
      {
         foreach (KnowledgeEntity.Category child in category)
         { %>
         <ul>
            <li>
            <div>
               <%=Html.Hidden(child.ID + "", child.ID) %>
               <a href="#"><%=child.Name %></a>
            </div>
            <%
            renderCategories(child); %>
            </li>
         </ul>
         <%}
      } 
   }; %>
<% KnowledgeEntity.Category rootCategory = ViewData["rootCategory"] as KnowledgeEntity.Category; %>
<h2>编辑条目</h2>
<div class="category-select" id="categories">
   <p>点击分类列表中的项目对条目的分类进行修改：</p>
   <% renderCategories(rootCategory); %>
</div>
<div class="article-edit">
<% Html.BeginForm(string.Empty, "Article", FormMethod.Post, new { @class = "article-main" });%>
   <dl>
      <% KnowledgeEntity.Article articleInView;
         if (ViewData["CurrentArticle"] != null)
         {
            articleInView = ViewData["CurrentArticle"] as KnowledgeEntity.Article;
         }
         else
         {
            articleInView = new KnowledgeEntity.Article
            {
               ArticleID = -1,
               Category = new KnowledgeEntity.Category(-1, string.Empty),
               Content = string.Empty,
               Title = string.Empty,
               VersionID = 0
            };
         } %>
      <dt>题目: </dt>
      <dd>
         <%= Html.Hidden("ID",articleInView.ArticleID) %>
         <%= Html.Hidden("VersionID",articleInView.VersionID) %>
         <%= Html.TextBox("Title", articleInView.Title)%></dd>
      <dd>
         <span id="TitleValidator" class="field-validation-error"></span>
      </dd>
      <dt>分类：</dt>
      <dd>
         <%= Html.Hidden("CategoryID", articleInView.Category.ID) %>
         <%= Html.TextBox("CategoryName", articleInView.Category.Name, new { @readonly = "true" })%>
      </dd>
      <dt>内容: </dt>
      <dd>
         <%= Html.TextArea("Content", articleInView.Content)%>
      </dd>
   </dl>
   <input type="submit" id="SaveDraft" value="保存草稿" name="SaveDraft" />
   <input type="submit" id="Commit" value="提交修改" name="Submit"/>
   <input type="submit" id="Cancel" value="放弃编辑" name="Cancel" />
<% Html.EndForm(); %>
</div>
</asp:Content>
